//
//  OurAppsTableViewController.m
//  AboutUs
//
//  Created by Richa Pahwa on 2/10/13.
//  Copyright (c) 2013 Richa Pahwa. All rights reserved.
//

#import "OurAppsTableViewController.h"
#import "AppDetails.h"
#import "AppDetailsViewController.h"
#import "AppDetailRestBridge.h"
#import "AppDetailTableCell.h"

@implementation OurAppsTableViewController

@synthesize appDetails;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    AppDetailRestBridge *restBridge = [AppDetailRestBridge sharedInstance];
    [restBridge getAllApplicationsFromUrl:@"http://127.0.0.1:8080/our-apps/rest/appDetails" withCompletionHandler:^(NSData * response, NSError* error){
        dispatch_async(dispatch_get_main_queue(), ^{
            if (!error) {
                NSError* jsonSerializationError;
                
                appDetails = [NSJSONSerialization JSONObjectWithData:response
                                                             options:NSJSONReadingMutableContainers
                                                               error:&jsonSerializationError];
                if ( [NSJSONSerialization isValidJSONObject:appDetails] )
                {
                    NSLog(@"Loaded app details %d",appDetails.count);
                    [self.tableView reloadData];
                }
            }
        });
    }];    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"Number of rows in table view %d",appDetails.count);
    return appDetails.count;
}


- (AppDetailTableCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dictionary = [appDetails objectAtIndex:[indexPath row]];
    static NSString *CellIdentifier = @"AppsTableCell";
    AppDetailTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if ( !cell ) {
        cell = [[AppDetailTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSLog(@"Item at %d has app name: %@",indexPath.row,[dictionary valueForKey:@"applicationName"]);
    cell.textLabel.text = [dictionary valueForKey:@"applicationName"];
    cell.detailTextLabel.text = [dictionary valueForKey:@"applicationDescribe"];
    return cell;
}

#pragma mark - Table view delegate

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ( [segue.identifier isEqualToString:@"AppDetailsInfoPush"] )
    {
        NSString *name;
        NSString *description;
    
        NSInteger row = [[self tableView].indexPathForSelectedRow row];
        NSDictionary* applicationDictionary = [appDetails objectAtIndex:row];
        name = [applicationDictionary valueForKey:@"applicationName"];
        description = [applicationDictionary valueForKey:@"applicationDescribe"];
    
        AppDetailsViewController *controller = segue.destinationViewController;
        controller.appDetails = [[AppDetails alloc] initWithName:name description:description];

    }
}
@end
